package com.cw.jeeyt.common.utils;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;

/**
 * Spring Bean管理器，可以从beanFactory中根据bean名称获取bean
 *
 * @author fengmq
 */
public class SpringBeanManager implements BeanFactoryAware {
    private static BeanFactory beanFactory = null;

    public static BeanFactory getBeanFactory() {
        return beanFactory;
    }

    public void setBeanFactory(BeanFactory bFactory) throws BeansException {
        if (beanFactory == null) {
            beanFactory = bFactory;
        }
    }

    public static <T> T getBean(Class<T> clazz) {
        if (beanFactory != null) {
            return beanFactory.getBean(clazz);
        }
        return null;
    }

    public static Object getBean(String beanName) {
        if (beanFactory != null) {
            return beanFactory.getBean(beanName);
        }
        return null;
    }
}
